#!/bin/bash
# chkconfig: 2345 93 11
# description:Nginx Server

NGINX_HOME=/usr/local/nginx
NGINX_SBIN=$NGINX_HOME/sbin/nginx
NGINX_CONF=$NGINX_HOME/conf/nginx.conf
NGINX_PID=$NGINX_HOME/logs/nginx.pid
NGINX_MAXFD=65535

NGINX_NAME="Nginx"

#. /etc/rc.d/init.d/functions

if [ ! -f $NGINX_SBIN ]
then
	echo "$NGINX_NAME startup: $NGINX_SBIN not exists! "
	exit
fi

start() {
	ulimit -HSn $NGINX_MAXFD
	
	$NGINX_SBIN -c $NGINX_CONF
	ret=$?
	if [ $ret -eq 0 ]; then
#		action $"Starting $NGINX_NAME: " /bin/true
		echo "Starting nginx OK"
	else
		echo "Starting nginx NO"
#		action $"Starting $NGINX_NAME: " /bin/false
	fi
}

stop() {
	kill $(cat $NGINX_PID)
	ret=$?
	if [ $ret -eq 0 ]; then
#		action $"Stopping $NGINX_NAME: " /bin/true
		echo "Stopping nginx OK"
	else
#		action $"Stopping $NGINX_NAME: " /bin/false
		echo "Stopping nginx NO"
	fi
}

restart() {
	stop
	sleep 2
	start
}

check() {
	$NGINX_SBIN -c $NGINX_CONF -t
}

reload() {
	kill -HUP $(cat $NGINX_PID)
}

relog() {
	kill -USR1 $(cat $NGINX_PID)
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		restart
		;;
	check)
		check
		;;
	reload)
		reload
		;;
	relog)
		relog
		;;
	*)
		echo $"Usage: $0 {start|stop|restart|reload|check|relog}"
		exit 1
esac

